﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Globalization;
using System.Web.Mail;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace QianJin.Models
{
    public class UserInfo
    {
        public string Email { get; set; }
        public CultureInfo Market { get; set; }
        public ShoppingCart ShoppingCart { get; private set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string Comment { get; set; }
        public string Phone { get; set; }
        public string Company { get; set; }

        private static string smtp;
        private static string user;
        private static string pwd;
        private static int port;
        private static string to;

        public static string ToMailAddress
        {
            get
            {
                return to;
            }
        }

        public static void Init(string host, string username, string password, int smtp_port, string toAddress)
        {
            smtp = host;
            user = username;
            pwd = password;
            port = smtp_port;
            to = toAddress;
        }

        public UserInfo()
        {
            ShoppingCart = new ShoppingCart();
        }


        public string ToHtmlString()
        {
            StringBuilder h = new StringBuilder();
            h.Append("<div>");
            h.AppendFormat(CultureInfo.InvariantCulture, "<div>Name:{0}</div>", this.Name);
            h.AppendFormat(CultureInfo.InvariantCulture, "<div>Email:{0}</div>", this.Email);
            h.AppendFormat(CultureInfo.InvariantCulture, "<div>Address:{0}</div>", this.Address);
            h.AppendFormat(CultureInfo.InvariantCulture, "<div>Phone:{0}</div>", this.Phone);
            h.AppendFormat(CultureInfo.InvariantCulture, "<div>Company:{0}</div>", this.Company);
            h.AppendFormat(CultureInfo.InvariantCulture, "<div>Comment:{0}</div>", this.Comment);

            foreach (var prod in this.ShoppingCart.ShoppingItems)
            {
                h.AppendFormat(CultureInfo.InvariantCulture, "<div><div>prod: {0}</div><div>id: {1}</div></div>", prod.Name, prod.id.ToString());
            }

            h.Append("</div>");

            return h.ToString();
        }
        
        public override string ToString()
        {
            StringBuilder h = new StringBuilder();
            h.AppendFormat(CultureInfo.InvariantCulture, "Name:{0} \r\n", this.Name);
            h.AppendFormat(CultureInfo.InvariantCulture, "Email:{0} \r\n", this.Email);
            h.AppendFormat(CultureInfo.InvariantCulture, "Address:{0} \r\n", this.Address);
            h.AppendFormat(CultureInfo.InvariantCulture, "Phone:{0} \r\n", this.Phone);
            h.AppendFormat(CultureInfo.InvariantCulture, "Company:{0} \r\n", this.Company);
            h.AppendFormat(CultureInfo.InvariantCulture, "Comment:{0} \r\n", this.Comment);

            foreach (var prod in this.ShoppingCart.ShoppingItems)
            {
                h.AppendFormat(CultureInfo.InvariantCulture, "prod: {0} id: {1} \n", prod.Name, prod.id.ToString());
            }

            h.Append(" \n");

            return h.ToString();
        }

        public void SendOrderEmail()
        {
            if(this.ShoppingCart.ShoppingItems.Count == 0)
            {
                return;
            }
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            message.To.Add(to);
            message.Subject = "Order";
            message.From = new System.Net.Mail.MailAddress(user);
            message.Body = this.ToHtmlString();
            message.IsBodyHtml = true;
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
            client.Host = smtp;
            client.Port = port;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(user, pwd);
            client.EnableSsl = true;
            client.Send(message);
        }
    }
}
